package Views
{
	import Events.SnakeChangeEvent;
	
	import Player.SnakeHead;
	
	import flash.display.Sprite;
	
	import mx.core.UIComponent;

	/**
	 * Class to display a snake on screen
	 */
	public class SnakeView extends UIComponent
	{		
		private var field:Field;
		public function SnakeView()
		{
		}
		
		/**
		 * Function to draw the snake head
		 * Head can be Rectangular or round
		 * Head can be normal or inverted color
		 */
		public function updateSnakeHead(snakeChangeEvent:SnakeChangeEvent):void{
			var snake:SnakeHead = snakeChangeEvent.snake;
			
			// Check if snake can be drawed
			if(snake != null){
				// Set head color
				graphics.lineStyle(snake.thickness,snake.headColor);
				
				// Determine to draw round or rectangular head
				if(snake.roundHead){
					graphics.drawEllipse(-(snake.thickness/2),-(snake.thickness/2),snake.thickness,snake.thickness);
				}else{
					graphics.drawRect(-(snake.thickness/2),-(snake.thickness/2),snake.thickness,snake.thickness);
				}
			}
		}
		
		/**
		 * Function to draw the colored tail on a snake
		 * after it has moved. The tail will be directly drawn on the field.
		 */
		public function updateAfterMove(snakeChangeEvent:SnakeChangeEvent):void{
			var snake:SnakeHead = snakeChangeEvent.snake;
			
			// Check if snake can be drawed
			if(snake != null){	
				// Update snake head position
				this.x = snake.position.x;
				this.y = snake.position.y;
				
				if(parent is Field){
					var parentSprite:Field = Field(parent);
					parentSprite.graphics.lineStyle(snake.thickness, snake.tailColor);
					
					// Move to snake position and draw line piece
					parentSprite.graphics.moveTo(snake.position.x,snake.position.y);
					parentSprite.graphics.lineTo(snakeChangeEvent.oldPosition.x,snakeChangeEvent.oldPosition.y);
				}
			}
		}
	}
}